package markers;

import common.ChartDemo;
import common.ChartUtil;
import common.CircleDrawer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYDrawableAnnotation;
import org.jfree.chart.annotations.XYPointerAnnotation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.ui.LengthAdjustmentType;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.data.general.Dataset;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;

import javax.swing.text.TextAction;
import java.awt.*;

public class MarkerDemo1 extends ChartDemo {
    public MarkerDemo1(String title) {
        super(title);

        ChartPanel chartPanel = super.getChartPanel();
        chartPanel.setDomainZoomable(true);
        chartPanel.setRangeZoomable(true);
    }

    @Override
    protected Dataset createDataset() {
        TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
        timeSeriesCollection.addSeries(createSupplier1Bids());
        timeSeriesCollection.addSeries(createSupplier2Bids());
        return timeSeriesCollection;
    }

    private TimeSeries createSupplier1Bids() {
        Hour hour = new Hour(1, new Day(22, 5, 2003));
        TimeSeries timeseries = new TimeSeries("Supplier 1");
        timeseries.add(new Minute(13, hour), 200D);
        timeseries.add(new Minute(14, hour), 195D);
        timeseries.add(new Minute(45, hour), 190D);
        timeseries.add(new Minute(46, hour), 188D);
        timeseries.add(new Minute(47, hour), 185D);
        timeseries.add(new Minute(52, hour), 180D);
        return timeseries;
    }

    private TimeSeries createSupplier2Bids() {
        Hour hour = new Hour(1, new Day(22, 5, 2003));
        Hour hour1 = (Hour)hour.next();
        TimeSeries timeseries = new TimeSeries("Supplier 2");
        timeseries.add(new Minute(25, hour), 185D);
        timeseries.add(new Minute(0, hour1), 175D);
        timeseries.add(new Minute(5, hour1), 170D);
        timeseries.add(new Minute(6, hour1), 168D);
        timeseries.add(new Minute(9, hour1), 165D);
        timeseries.add(new Minute(10, hour1), 163D);
        return timeseries;
    }

    @Override
    protected JFreeChart createChart(Dataset dataset) {
        XYDataset xyDataset = (XYDataset) dataset;
        JFreeChart jFreeChart = ChartFactory.createScatterPlot("Marker Demo 1", "X", "Y",
                xyDataset, PlotOrientation.VERTICAL, true, true, false);
        LegendTitle legendTitle = (LegendTitle) jFreeChart.getSubtitle(0);
        legendTitle.setPosition(RectangleEdge.RIGHT);

        XYPlot xyPlot = (XYPlot) jFreeChart.getPlot();
        xyPlot.setDomainPannable(true);
        xyPlot.setRangePannable(true);
        xyPlot.getRenderer().setDefaultToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());

        DateAxis dateAxis = new DateAxis("Time");
        dateAxis.setUpperMargin(0.5D);
        xyPlot.setDomainAxis(dateAxis);

        ValueAxis valueAxis = xyPlot.getRangeAxis();
        valueAxis.setUpperMargin(0.29999999999999999D);
        valueAxis.setLowerMargin(0.5D);

        ValueMarker valueMarker = new ValueMarker(200D);
        valueMarker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        valueMarker.setPaint(Color.green);
        valueMarker.setLabel("Bid Start Price");
        valueMarker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
        valueMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        xyPlot.addRangeMarker(valueMarker);

        ValueMarker valueMarker1 = new ValueMarker(175D);
        valueMarker1.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        valueMarker1.setPaint(Color.red);
        valueMarker1.setLabel("Target Price");
        valueMarker1.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
        valueMarker1.setLabelTextAnchor(TextAnchor.BOTTOM_RIGHT);
        xyPlot.addRangeMarker(valueMarker1);

        Hour hour = new Hour(2, new Day(22, 5, 2003));
        double d = hour.getFirstMillisecond();
        ValueMarker valueMarker2 = new ValueMarker(d);
        valueMarker2.setPaint(Color.orange);
        valueMarker2.setLabel("Original Close (02:00)");
        valueMarker2.setLabelAnchor(RectangleAnchor.TOP_LEFT);
        valueMarker2.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        xyPlot.addDomainMarker(valueMarker2);

        Minute minute = new Minute(15, hour);
        d = minute.getFirstMillisecond();
        ValueMarker valueMarker3 = new ValueMarker(d);
        valueMarker3.setPaint(Color.red);
        valueMarker3.setLabel("Close Date (02:15)");
        valueMarker3.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
        valueMarker3.setLabelTextAnchor(TextAnchor.TOP_LEFT);
        xyPlot.addDomainMarker(valueMarker3);

        Hour hour1 = new Hour(2, new Day(22, 5, 2003));
        Minute minute1 = new Minute(10, hour1);
        d = minute1.getFirstMillisecond();
        CircleDrawer circleDrawer = new CircleDrawer(Color.red, new BasicStroke(1.0F), null);
        XYDrawableAnnotation xyDrawableAnnotation = new XYDrawableAnnotation(d, 163D, 11D, 11D, circleDrawer);
        xyPlot.addAnnotation(xyDrawableAnnotation);

        XYPointerAnnotation xyPointerAnnotation = new XYPointerAnnotation("Best Bid", d, 163D, 2.3561944901923448D);
        xyPointerAnnotation.setBaseRadius(35D);
        xyPointerAnnotation.setTipRadius(10D);
        xyPointerAnnotation.setFont(new Font("SansSerif", 0, 9));
        xyPointerAnnotation.setPaint(Color.blue);
        xyPointerAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_RIGHT);
        xyPlot.addAnnotation(xyPointerAnnotation);

        ChartUtils.applyCurrentTheme(jFreeChart);
        return jFreeChart;
    }

    public static void main(String[] args) {
        MarkerDemo1 demo1 = new MarkerDemo1("MarkerDemo1");
        ChartUtil.showChartDemo(demo1);
    }
}
